<?php
set_time_limit(0);

include_once('lib/config.php');
include_once('lib/Db.class.php');
include_once('lib/Model.class.php');
include_once('lib/Upyun.class.php');
include_once('lib/Image.class.php');
include_once('lib/Http.class.php');
include_once('lib/Cut.class.php');
include_once('lib/function.php');

$redis = new Redis();
$redis->connect(REDIS_IP,"6379");  //php客户端设置的ip及端口
if(!$redis) die('redis connect fail');

$queue_zlist_fetch_key = 'Queue_zcopy_images_fetch';
$mh_list_images = 'MH_ZCOPY_LIST_IMAGES';

$model = new Model;
$http  = new Http;
$cut   = new Cut;

$pid = getmypid();
$redis->hSet(MH_PIDS, $pid, date('Y-m-d H:i:s').'||mh_zcopy_images');

$limit = 50;
while($limit--) {
  $stop = $redis->get('LNPAN_MH_QUEUE_STOP');
  if($stop) {
    die('stop');
  }
  
  $is_images_continue = false;
  $result = array();
  $queue = $redis->lPop($queue_zlist_fetch_key);
  if($queue) {
    $info = unserialize($queue);
    if(!$info) {
      break;
    }

    $urls_enough = false;
    $urls = array();
    $chapters = json_decode($info['chapter'], true);
    foreach ($chapters as $title => $list) {
      if($urls_enough) break;
      foreach ($list as $url => $tit) {
        $url = str_replace(' ', '%20', $url);
        $url_key = getMD5($url);
        //$old = $model->find('news_images', "url_key='$url_key'");
        $old = $redis->hGet($mh_list_images, $url_key);
        if(empty($old)) {
          $urls[] = $url;
          if(count($urls) >= 10) {
            $urls_enough = true;
            break;
          }
        } else {
          $result['exists_url'][$url_key] = $url;
        }
      }
    }
    $result['id'] = $info['id'];
    $result['link'] = $info['link'];

    if(count($urls) > 0) {
      $link = $info['link'];
      $parse_link = parse_url($link);
      $opt_data = array(
        CURLOPT_REFERER => $link,
        CURLOPT_ENCODING=> 'gzip',
        CURLOPT_HTTPHEADER => array(
          'Referer' => $link,
          'Host' => $parse_link['host'],
          'Pragma' => 'no-cache',
        ),
      );
      $http->opt = $opt_data;
      $responses = $http->curl_http($urls);
      foreach ($responses as $url => $item) {
        $data = $cut->getImagesUrl_ByFile($info, $item);
        if(is_array($data) && count($data) > 0) {
          $new_data = array(
            'url' => $url,
            'news_id' => $info['id'],
            'url_key' => getMD5($url),
            'images1' => json_encode($data),
            'images1_count' => count($data),
          );
          $ret = $model->add('news_images', $new_data);
          $mysql_error = mysql_error();
          if($ret) {
            $news_images_id = $model->insert_id();
            $redis->hSet($mh_list_images, $new_data['url_key'], $info['id'].':'.$news_images_id.':'.$new_data['url']);
            $is_images_continue = true;
          } elseif(stristr($mysql_error, 'Duplicate')) {
            $result['exists'][] = $info['id'].':'.$new_data['url'].':'.$mysql_error;
            $redis->hSet($mh_list_images, $new_data['url_key'], $info['id'].':'.$mysql_error);
            $is_images_continue = true;
          }

        } else {
          unset($item['results']);
          $result[$url] = $item;
          $updata = array();
          if($item['info']['http_code'] == 404) {
            //$updata = array('list_state'=>2, 'news_state'=>0);
            $updata = array('uptime'=>time());
          }
          $updata['list_state'] = 2;

          $model->save("news_zcopy", "id='$info[id]'", $updata);
        }
      }
      $counter = $model->query("select count(*) as count from news_images where news_id='$info[id]'");
      $result['progress'] = $counter[0]['count'].'/'.$info['chapter_count'];
    }
    else {
      //$counter = $model->query("select count(*) as count from news_images where news_id='$info[id]'");
      //if($counter[0]['count'] >= $info['chapter_count']) {
      //$left_chapter = substr($info['chapter'], 0, 4);
      //$news_state = $left_chapter == '{"\u' ? 1 : 9;
      $news_state = 1;
      $updata = array('list_state'=>1, 'news_state'=>$news_state);
      $model->save("news_zcopy", "id='$info[id]'", $updata);
      $result['success'] = $info;
      //}
      /*
      else {
        $result['progress'] = $counter[0]['count'].'/'.$info['chapter_count'];
        /数据重复
        $re_info = $model->find("news_zcopy", "link='$info[link]' and id<>'$info[id]'");
        if($re_info) {
          if($info['chapter_count'] >= $re_info['chapter_count']) {
            $model->query("delete from news_zcopy where id='$re_info[id]'");
            $model->query("update news_images set news_id='$info[id]' where news_id='$re_info[id]'");
            $is_images_continue = true;
          }
          else {
            $model->query("delete from news_zcopy where id='$info[id]'");
            $model->query("update news_images set news_id='$re_info[id]' where news_id='$info[id]'");
          }
        }   
      }*/
    }

    if($is_images_continue) {
      $redis->lPush($queue_zlist_fetch_key, $queue);
    }
    elseif (!isset($result['success'])) {
      $updata = array('list_state'=>0);
      $model->save("news_zcopy", "id='$info[id]'", $updata);
    }
  }
  
  //print_r($result);
  //break;
}

$model->close();
$redis->hDel(MH_PIDS, $pid);
